package newcrunch.security;

import java.io.FileDescriptor;
import java.io.IOException;
import java.net.InetAddress;
import java.security.Permission;
import java.util.Properties;
import jsyntaxpane.util.JarServiceProvider;
import newcrunch.grader.AutoTestManager;
import newcrunch.util.Logging;

/**
 * Class to neutralize any malicious code from the student.
 * @author WL
 */
public class CustomSecurityManager extends SecurityManager
{    
    private static final String filename = "newcrunch/security/autotester.properties";
    private static Properties props = new Properties();
    
    static
    {
        try {
            props.load(JarServiceProvider.findResource(filename));
        } catch (IOException exp) {
            Logging.log(exp);
        }
    }
    
    private boolean isTestThread()
    {
        try {
            return AutoTestManager.TEST_THREAD.equals(Thread.currentThread().getName());
        } catch (Exception exp) {
            return false;
        }
    }
    
    private void checkMethod(String name)
    {
        if (isTestThread() && "true".equalsIgnoreCase(props.getProperty(name)))
            throw new SecurityException(props.getProperty(name + ".msg"));
    }
    
    @Override
    public void checkAccept(String host, int port) 
    {
        checkMethod("checkAccept");
    }
    
    @Override
    public void checkAccess(ThreadGroup g)
    {
        checkMethod("checkAccess");
    }
    
    @Override
    public void checkAwtEventQueueAccess()
    {
        checkMethod("checkAwtEventQueueAccess");
    }
        
    @Override
    public void checkConnect(String host, int port)
    {
        checkMethod("checkConnect");
    }

    @Override
    public void checkConnect(String host, int port, Object context)
    {
        checkMethod("checkConnect");
    }

    @Override
    public void checkCreateClassLoader()
    {
        checkMethod("checkCreateClassLoader");
    }

    @Override
    public void checkDelete(String file)
    {
        checkMethod("checkDelete");
    }

    @Override
    public void checkExec(String cmd)
    {
        checkMethod("checkExec");
    }

    @Override
    public void checkExit(int status)
    {
        checkMethod("checkExit");
    }

    @Override
    public void checkLink(String lib)
    {
        checkMethod("checkLink");
    }

    @Override
    public void checkListen(int port)
    {
        checkMethod("checkListen");
    }

    @Override
    public void checkMemberAccess(Class<?> clazz, int which)
    {
        checkMethod("checkMemberAccess");
    }

    @Override
    public void checkMulticast(InetAddress maddr)
    {
        checkMethod("checkMulticast");
    }
    @Override
    public void checkPackageAccess(String pkg)
    {
        checkMethod("checkPackageAccess");
    }

    @Override
    public void checkPackageDefinition(String pkg)
    {
        checkMethod("checkPackageDefinition");
    }

    @Override
    public void checkPermission(Permission perm)
    {
        checkMethod("checkPermission");
    }

    @Override
    public void checkPermission(Permission perm, Object context)
    {
        checkMethod("checkPermission");
    }

    @Override
    public void checkPrintJobAccess()
    {
        checkMethod("checkPrintJobAccess");
    }

    @Override
    public void checkPropertiesAccess()
    {
        checkMethod("checkPropertiesAccess");
    }

    @Override
    public void checkPropertyAccess(String key)
    {
        checkMethod("checkPropertyAccess");
    }

    @Override
    public void checkRead(FileDescriptor fd)
    {
        checkMethod("checkRead");
    }

    @Override
    public void checkRead(String file)
    {
        checkMethod("checkRead");
    }

    @Override
    public void checkRead(String file, Object context)
    {
        checkMethod("checkRead");
    }

    @Override
    public void checkSecurityAccess(String target)
    {
        checkMethod("checkSecurityAccess");
    }

    @Override
    public void checkSetFactory()
    {
        checkMethod("checkSetFactory");
    }

    @Override
    public void checkSystemClipboardAccess()
    {
        checkMethod("checkSystemClipboardAccess");
    }

    @Override
    public boolean checkTopLevelWindow(Object window)
    {
        checkMethod("checkTopLevelWindow");
        return true;
    }

    @Override
    public void checkWrite(FileDescriptor fd)
    {
        checkMethod("checkWrite");
    }

    @Override
    public void checkWrite(String file)
    {
        checkMethod("checkWrite");
    }
}
